package a06_栈与队列;

import java.util.LinkedList;
import java.util.Queue;

/**
 * <p>
 * a02_用队列实现栈
 * offer添加
 * poll删除
 * </p>
 *
 * @author flyduck
 * @since 2024-07-25
 */
public class a03_用队列实现栈复习1 {
    public static void main(String[] args) {
        MyStack1 myStack1 = new MyStack1();
        myStack1.push(1);
        myStack1.push(2);
        myStack1.top();
        myStack1.pop();
        System.out.println(myStack1.empty());

    }
}

class MyStack2 {

    private Queue<Integer> queue;

    public MyStack2() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
        queue.add(x);
    }

    public int pop() {
        int size = queue.size();
        while (size - 1 > 0) {
            size--;
            queue.add(queue.poll());
        }
        return queue.poll();
    }

    public int top() {
        int size = queue.size();
        while (size - 1 > 0) {
            size--;
            queue.add(queue.poll());
        }
        Integer poll = queue.poll();
        queue.add(poll);
        return poll;
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}
